package com.novel.filter;


import com.mysql.cj.log.Log;
import com.novel.config.RedissonHelper;
import com.novel.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 权限验证
 *
 * @author chihuo
 * @date 2022/04/05
 */
@Slf4j
@Order
@Component
public class AuthFilter implements Filter {

    @Resource
    private RedissonHelper redissonHelper;

    private static final List<String> WHITE_LIST = Arrays.asList(
           "/user/login",
            "novel/page",
            "/doc.html",
            "/favicon.ico",
            "/user/getEmailCode",
            "/user/register",
            "/file/upload",
            "/novel/getAllClassify",
            "/novel/getNovelList",
            "/user/failed",
            "/novel/readNum",
            "/novel/read",
            "/novel/getNovelInfo",
            "/novel/getAllChapter",
            "/novel/search",
            "/comment/getCommentList",
            "/comment/getAllReply",
            "/comment/getNovelCommentAll"
    );


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        String path = req.getServletPath();
        //放行swagger相关路由
        if (path.contains("swagger") || path.contains("webjars") || path.contains("v2")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        //白名单过滤
        for (String s : WHITE_LIST) {
            if (CommonUtil.checkStart(s, path)) {
                String token = req.getHeader("token");
                if(StringUtils.isNotEmpty(token)){
                    HttpSession session = req.getSession();
                    session.setAttribute("token", token);
                }
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            }
        }

        String token = req.getHeader("token");
        if (StringUtils.isEmpty(token) || ObjectUtils.isEmpty(redissonHelper.getValue("novel_user" + token))) {
            //跳转失败
            log.error("请求"+path+"被拦截，未登录");
            servletRequest.getRequestDispatcher("/user/failed").forward(servletRequest, servletResponse);
        }else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
